$(function () {
  $(".tab3").click(function () {
    loadGridTable2();
  });
});

function loadGridTable2() {
  $("#promot_gridTable").jqGrid({
    url: "items.action?approveStatus="+$("#approve_status_2").val(),
    datatype: "json",
    height: 650,
    width: 750,
    colNames: ["编号", "图片", "宝贝", "价格", "下架时间", "pic_url","title"],
    colModel: [{
      name: "iid",
      index: "iid",
      hidden: true
    },
    {
      name: "pic_url",
      index: "pic_url",
      formatter:formatItemPic,
      width: "60",
      align:"center",
      sortable: false
    },
    {
      name: "title",
      index: "title",
      formatter:formateadorLink,
      width: "350"
    },
    {
      name: "price",
      index: "price",
      sorttype: "int",
      width: "60"
    },
    {
      name: "delist_time",
      index: "delist_time",
      width: "160"
    },
    {
      name: "pic_url",
      index: "pic_url",
      hidden: true
    },
    {
      name: "frank_title",
      index: "frank_title",
      hidden: true
    }],
    sortname: "delist_time",
    sortorder: "desc",
    viewrecords: true,
    rowNum: 10,
    rowList: [10, 20, 30],
    multiselect: true,
    multiselectWidth: 50,
    loadComplete: function () {
      setPromotGridTableSelection();
      setImageCellHeight('promot_gridTable');
    },
    onSelectRow: function (rowid, status) {
      $("#accordion").accordion("option", "active", 1);
      if (rowid === null) {
        return;
      }
      var ret = $("#promot_gridTable").jqGrid("getRowData", rowid);
      var iid = "piid" + ret.iid;
      var oldsrc = $("#" + iid + " img").attr("src");
      if (status) {
        if (oldsrc != null) {
          return;
        }
        var title_html = builderSimpleTitle(ret);
        var onClickButtonHtml ="<input type='button' class='btn2 pbtn1' onmouseout=\"this.style.borderColor='#dcdcdc'\" onmouseover=\"this.style.borderColor='#f76b00'\" value='删除' style=\"border-color: rgb(220, 220, 220);\"  onclick=\"onClickDelete_Step2('" + iid + "')\">"
        var result = "<div id='" + iid + "' class='piids_promot'><img src='" + ret.pic_url + "'><br/>" + title_html + "<br/>" + onClickButtonHtml + "</div>";
        $("#promot_resp").append(result);
      } else {
        $("#" + iid).remove();
      }
      transPromoteToServer();
    },
    onSelectAll: function (ids, status) {
      $("#accordion").accordion("option", "active", 1);
      if (ids == null || ids.length == 0) {
        return;
      }
      if (status) {
        for (i = 0; i < ids.length; i++) {
          var ret = $("#promot_gridTable").jqGrid("getRowData", ids[i]);
          var iid = "piid" + ret.iid;
          var oldsrc = $("#" + iid + " img").attr("src");
          if (oldsrc == null) {
            var title_html = builderSimpleTitle(ret);
            var onClickButtonHtml ="<input type='button' class='btn2 pbtn1' onmouseout=\"this.style.borderColor='#dcdcdc'\" onmouseover=\"this.style.borderColor='#f76b00'\" value='删除' style=\"border-color: rgb(220, 220, 220);\"  onclick=\"onClickDelete_Step2('" + iid + "')\">"
            var result = "<div id='" + iid + "' class='piids_promot'><img src='" + ret.pic_url + "'><br/>" + title_html + "<br/>" + onClickButtonHtml + "</div>";
            $("#promot_resp").append(result);
          }
        }
      } else {
        for (i = 0; i < ids.length; i++) {
          var ret = jQuery("#promot_gridTable").jqGrid("getRowData", ids[i]);
          $("#piid" + ret.iid).remove();
        }
      }
      transPromoteToServer();
    },
    jsonReader: {
      root: "dataset",
      page: "pager.curPageNo",
      total: "pager.totalPages",
      records: "pager.totalCount",
      repeatitems: false
    },
    prmNames: {
      rows: "pager.pageSize",
      page: "pager.curPageNo",
      sort: "pager.orderBy",
      order: "pager.order"
    },
    pager: "#promot_gridPager",
    caption: "安装到哪些宝贝上"
  });
}

$(function () {
  var beCheckedPromot = $("input[name=promot]:checked").val();
  if (beCheckedPromot != "custom") {
    $("#promot_grid").hide();
  } // 隐藏自定义选择用的grid
  if (beCheckedPromot != "someCategory") {
    $("#seller_cats_promote_div").hide();
  }
});

$(function () {
  $("#custom").click(function () {
    $("#seller_cats_promote_div").hide();
    $("input[name=seller_cats_promote]:checked").each(function(){
      $(this).attr('checked','');
    });  
    $("#promot_grid").show(); // 显示自定义选择用的grid
  });
  
  $("#someCategory").click(function () {
    $("#seller_cats_promote_div").show();
    $("#promot_grid").hide();
    $(".piids_promot").remove(); // 删除自定义方式选择的宝贝
    $("#promot_gridTable").jqGrid("resetSelection");
  });
  
  $(".notCustom").click(function () { // 当选择非自定义【所有宝贝、所有上架宝贝、所有仓库中的宝贝】时
    $("#promot_grid").hide(); // 隐藏自定义选择用的grid
    $(".piids_promot").remove(); // 删除自定义方式选择的宝贝
    $("#promot_gridTable").jqGrid("resetSelection");
    $("#seller_cats_promote_div").hide();
    $("input[name=seller_cats_promote]:checked").each(function(){
       $(this).attr('checked','');
    });  
  });
  
});

$(function() {
  $('#promot_searchKey').keypress(function(event) {
    if ((event.keyCode == 13)) {// 在这里接收的是Enter事件,S的ASCII码为13。
       doSearch2();
    }
  });
    
  $('#promot_searchKey').blur(function(){
    doSearch2();
  });
  
  $("#seller_cats_2").change(function () {
   doSearch2();
  });
  
  $("#approve_status_2").change(function () {
    doSearch2();
  });
  
});

function doRefresh2() {
  jQuery("#promot_gridTable").jqGrid("setGridParam", {
    url: "items.action?refresh=true",
    page: 1
  }).trigger("reloadGrid");
}


function setPromotGridTableSelection() {
  var rownum = $("#promot_gridTable").jqGrid("getGridParam", "rowNum");
  if(rownum==null){
    return;
  }
  $("#promot_gridTable").jqGrid("resetSelection");
  $(".piids_promot").each(function () {
    var iid = $(this).attr("id");
    for (i = 1; i <= rownum; i++) {
      var ret = $("#promot_gridTable").jqGrid("getRowData", i);
      if ("piid" + ret.iid == iid) {
        $("#promot_gridTable").setSelection(i);
      }
    }
  });
}

function onClickDelete_Step2(id) {
  $("#" + id).remove();
  setPromotGridTableSelection();
  transPromoteToServer();
}

function doSearch2() {
  var searchKey = jQuery("#promot_searchKey").val();
  searchKey = encodeURI(encodeURI(searchKey));
  jQuery("#promot_gridTable").jqGrid("setGridParam", {
    url: "items.action"+$("#jsession_id").val()+"?cid=" + $('#seller_cats_2').val()+"&approveStatus="+$("#approve_status_2").val()+"&searchKey="+searchKey,
    page: 1
  }).trigger("reloadGrid");
}

